import localForage from 'localforage/src/localforage';
export default {
	// 收到的消息保存至本地 whichPath
	// listStrName 未用到
	async gotMessage (which, res, userId, vm, listStrName) {
		// console.log('收到服务器内容：', res);
		let list = JSON.parse(res.data);
		// console.log('list: ', list);
		if (list.length > 0) {
			let sysnoticeList = [],
				couponList = [],
				printmsgList = [],
				p2pList = [];
			for (let obj of list) {
				let v = JSON.parse(obj);
				if (v.msgType === 'p2p') {
					p2pList.push({
						id: v.message.id,
						uid: v.message.uid,
						// username: v.from,
						username: v.message.username,
						face: v.message.face || 1,
						time: v.message.time,
						type: v.message.type,
						msg: v.message.msg,
					});
				} else if (v.msgType === 'sysnotice') { // 系统通知消息
					// let t = {
					// 	title: v.message
					// };
					let t = JSON.parse(v.message);
					sysnoticeList.push(t);
				} else if (v.msgType === 'coupon') { // 优惠劵 ，广告 等消息(TODO：优惠促销后台还未有)
					let t = JSON.parse(v.message);
					couponList.push(v.message);
				} else if (v.msgType === 'printmsg') { // 征订需求消息
					let t = JSON.parse(v.message);
					let spec = JSON.parse(t.CommoditySpec);
					let str = '';
					for (let x in spec) {
						str += `${x}: ${spec[x]} `;
					}
					t['CommoditySpec'] = str;
					printmsgList.push(t);
				}
			}
			// console.log('p2pList: ', p2pList);
			// console.log('sysnoticeList: ', sysnoticeList);
			// console.log('printmsgList: ', printmsgList);
			if (p2pList.length > 0) {
				console.group('socketCom - p2pList')
				for (let v of p2pList) {
					if (which === 'chat') {
						vm.screenMsg(v);
					} else {
						// chatList
						if (which === 'allNotice') {
							vm.setMsgDot(v);
						}
						await this.doLocalForageOutline(v, v.uid, userId);
					}
					await this.doLocalForage(v, v.uid, userId);
					// if (whichPath === 'chatPath') {
					// 	console.log('来自聊天界面，1.页面变化，2.存储本地');
					// 	vm.screenMsg(v);
					// 	await this.doLocalForage(v, v.uid, userId);
					// } else if (whichPath === 'msgPath') {
					// 	console.log('来自消息列表界面，1.页面未读消息变化，2.存储本地（包含未读和所有消息）');
					// 	vm.setMsgDot(v);
					// 	await this.doLocalForageOutline(v, v.uid, userId);
					// 	await this.doLocalForage(v, v.uid, userId);
					// } else if (whichPath !== 'noticePath' && whichPath !== 'notice2Path' && whichPath !== 'discountPath'){
					// 	console.log('来自非消息和各通知以及聊天界面的其他界面，1.存储本地（包含未读和所有消息）');
					// 	await this.doLocalForageOutline(v, v.uid, userId);
					// 	await this.doLocalForage(v, v.uid, userId);
					// }
				}
				console.groupEnd();
			}
			if (sysnoticeList.length > 0) {
				for (let v of sysnoticeList) {
					if (which === 'notice') {
						// vm[listStrName].push(v);
						vm['list'].push(v);
					} else {
						if (which === 'allNotice') {
							vm['sysnoticeList'].push(v);
						}
						await this.doLocalForageOutline2(v, 1, userId);
					}
					await this.doLocalForage2(v, 1, userId);
					
					// if (whichPath === 'noticePath') {
					// 	vm[listStrName].push(v);
					// 	await this.doLocalForage2(v, 1, userId);
					// } else if (whichPath === 'msgPath') {
					// 	vm['sysnoticeList'].push(v);
					// 	await this.doLocalForageOutline2(v, 1, userId);
					// } else if (whichPath !== 'chatPath' && whichPath !== 'notice2Path' && whichPath !== 'discountPath'){
					// 	await this.doLocalForageOutline2(v, 1, userId);
					// }
				}
			}
			if (couponList.length > 0) {
				for (let v of couponList) {
					if (which === 'coupon') {
						vm['list'].push(v);
					} else {
						if (which === 'allNotice') {
							vm['couponList'].push(v);
						}
						await this.doLocalForageOutline2(v, 3, userId);
					}
					await this.doLocalForage2(v, 3, userId);
				}
			}
			if (printmsgList.length > 0) {
				for (let v of printmsgList) {
					if (which === 'notice2') {
						vm['list'].push(v);
					} else {
						if (which === 'allNotice') {
							vm['printmsgList'].push(v);
						}
						await this.doLocalForageOutline2(v, 2, userId);
					}
					await this.doLocalForage2(v, 2, userId);
					
					// if (whichPath === 'notice2Path') {
					// 	vm[listStrName].push(v);
					// 	await this.doLocalForage2(v, 2, userId);
					// } else if (whichPath === 'msgPath') {
					// 	vm['printmsgList'].push(v);
					// 	await this.doLocalForageOutline2(v, 2, userId);
					// } else if (whichPath !== 'chatPath' && whichPath !== 'noticePath' && whichPath !== 'discountPath'){
					// 	await this.doLocalForageOutline2(v, 2, userId);
					// }
				}
			}
			if (which !== 'chat') {
				vm.setUnreadMsg(vm.unreadMsg + p2pList.length);
			}
			if (which !== 'notice') {
				vm.setUnreadMsg(vm.unreadMsg + sysnoticeList.length);
			}
			if (which !== 'notice2') {
				vm.setUnreadMsg(vm.unreadMsg + printmsgList.length);
			}
			if (which !== 'coupon') {
				vm.setUnreadMsg(vm.unreadMsg + couponList.length);
			}
		}
	},
	// 存储到本地 - 聊天消息
	async doLocalForage(obj, fromId, userId) {
		let indexDBMsgList = [];
		let msgAllIds = [];
		localForage.getItem('mgsAllIds').then((value) => {
			if (value) {
				msgAllIds = value;
			}
			if (msgAllIds.indexOf(`${fromId},${userId}`) === -1) {
				msgAllIds.push(`${fromId},${userId}`);
				localForage.setItem('mgsAllIds', msgAllIds);
			}
		}).catch((err) => {
			// 当出错时，此处代码运行
			console.log(err);
		});
		await localForage.getItem(`${fromId},${userId}`).then((value) => {
			// 当离线仓库中的值被载入时，此处代码运行
			// console.log('indexDBMsgList: ', value);
			if (value && value.length > 0) {
				indexDBMsgList = value;
			}
			indexDBMsgList.push(obj);
			localForage.setItem(`${fromId},${userId}`, indexDBMsgList).then((value) => {
				// 当值被存储后，可执行其他操作
				// console.log(value);
			}).catch((err) => {
				// 当出错时，此处代码运行
				console.log(err);
			});
		}).catch((err) => {
			// 当出错时，此处代码运行
			console.log(err);
		});
	},
	// 存储到本地 - 系统消息或者征订消息 type： 1 - 系统；2 - 征订；3 - 优惠
	async doLocalForage2 (obj, type = 1, userId) {
		let indexDBMsgList = [];
		let keyStr = `${userId}_notices`;
		if (type === 2) {
			keyStr = `${userId}_overprintNotices`;
		} else if (type === 3) {
			keyStr = `${userId}_coupon`;
		}
		await localForage.getItem(keyStr).then((value) => {
			// 当离线仓库中的值被载入时，此处代码运行
			// console.log('indexDBMsgList: ', value);
			if (value && value.length > 0) {
				indexDBMsgList = value;
			}
			indexDBMsgList.push(obj);
			localForage.setItem(keyStr, indexDBMsgList).then((value) => {
				// console.log(value);
			}).catch((err) => {
				console.log(err);
			});
		}).catch((err) => {
			console.log(err);
		});
	},
	// 存储到本地 - 聊天消息
	async doLocalForageOutline(obj, fromId, userId) {
		// console.log('outline 存储到本地 - 未读聊天消息');
		let indexDBMsgList = [];
		let mgsAllIdsOutline = [];
		localForage.getItem('mgsAllIdsOutline').then((value) => {
			if (value) {
				mgsAllIdsOutline = value;
			}
			if (mgsAllIdsOutline.indexOf(`outline_${fromId},${userId}`) === -1) {
				mgsAllIdsOutline.push(`outline_${fromId},${userId}`);
				localForage.setItem('mgsAllIdsOutline', mgsAllIdsOutline);
			}
		}).catch((err) => {
			// 当出错时，此处代码运行
			console.log(err);
		});
		await localForage.getItem(`outline_${fromId},${userId}`).then((value) => {
			// 当离线仓库中的值被载入时，此处代码运行
			// console.log('indexDBMsgList: ', value);
			if (value && value.length > 0) {
				indexDBMsgList = value;
			}
			indexDBMsgList.push(obj);
			localForage.setItem(`outline_${fromId},${userId}`, indexDBMsgList).then((value) => {
				// 当值被存储后，可执行其他操作
				// console.log(value);
			}).catch((err) => {
				// 当出错时，此处代码运行
				console.log(err);
			});
		}).catch((err) => {
			// 当出错时，此处代码运行
			console.log(err);
		});
	},
	// 存储到本地 - 离线消息（未读消息） - 系统消息或者征订消息 type： 1 - 系统；2 - 征订；3 - 优惠
	async doLocalForageOutline2 (obj, type = 1, userId) {
		// console.log('outline 存储到本地 - 未读系统消息或者征订消息');
		let indexDBMsgList = [];
		let keyStr = `${userId}_notices_outline`;
		if (type === 2) {
			keyStr = `${userId}_overprintNotices_outline`;
		} else if (type === 3) {
			keyStr = `${userId}_coupon_outline`;
		}
		await localForage.getItem(keyStr).then((value) => {
			// 当离线仓库中的值被载入时，此处代码运行
			// console.log('indexDBMsgList: ', value);
			if (value && value.length > 0) {
				indexDBMsgList = value;
			}
			indexDBMsgList.push(obj);
			localForage.setItem(keyStr, indexDBMsgList).then((value) => {
				// console.log(value);
			}).catch((err) => {
				console.log(err);
			});
		}).catch((err) => {
			console.log(err);
		});
	},
	async getNoticeUnreadMsg (userId, vm) {
		let s = 0;
		await localForage.getItem(`${userId}_notices_outline`).then((value) => {
			console.log('getIndexDBMsg: ', value);
			if (value && value.length > 0) {
				s += value.length;
			}
		}).catch((err) => {
			console.log(err);
		});
		await localForage.getItem(`${userId}_overprintNotices_outline`).then((value) => {
			console.log('getIndexDBMsg: ', value);
			if (value && value.length > 0) {
				s += value.length;
			}
		}).catch((err) => {
			console.log(err);
		});
		await localForage.getItem(`${userId}_coupon_outline`).then((value) => {
			console.log('getIndexDBMsg: ', value);
			if (value && value.length > 0) {
				s += value.length;
			}
		}).catch((err) => {
			console.log(err);
		});
		vm.setUnreadMsg(s);
	}
}